package com.halflife.netty_basics;

import com.halflife.netty_basics.common.CommonInboundHandler;
import com.halflife.netty_basics.common.CommonOutboundHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
    private ServerBootstrap serverBootstrap;
    private NioEventLoopGroup boss;
    private NioEventLoopGroup worker;

    public Server()  {
        serverBootstrap = new ServerBootstrap();
        boss = new NioEventLoopGroup(2);
        worker = new NioEventLoopGroup(3);
        serverBootstrap.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new CommonOutboundHandler()).addLast(new CommonInboundHandler());
            }
        });

        ChannelFuture channelFuture = serverBootstrap.bind("127.0.0.1",9090).syncUninterruptibly();
        try {
            channelFuture.channel().closeFuture().syncUninterruptibly();
        } finally {
            worker.shutdownGracefully();
            boss.shutdownGracefully();
        }

    }

    public static void main(String[] args) {
        new Server();
    }
}
